package org.ainlolcat.ainscience.entity;

import org.ainlolcat.ainscience.Controller;
import org.ainlolcat.ainscience.column.Column;
import org.ainlolcat.ainscience.column.ColumnImpl;
import org.ainlolcat.ainscience.project.Project;
import org.ainlolcat.ainscience.project.ProjectFileHandler;
import org.ainlolcat.ainscience.statistics.StatisticsCollectorImpl;
import org.ainlolcat.ainscience.templates.Template;
import org.ainlolcat.ainscience.tools;
import org.jdom.Element;

import java.io.IOException;
import java.util.*;

/**
 * Created by IntelliJ IDEA.
 * User: ain
 * Date: 12/6/11
 * Time: 4:36 AM
 * To change this template use File | Settings | File Templates.
 */
public class StatisticAggregatorEntityParser implements EntityParser{

    /**
     * almost all job does Entity. Parser only set basic parameters. It maiden for better update function
     * @param desc
     * @return
     */
    public Entity load(Element desc) {
        //<entity name="aggr" id="301" templateID="102" entityType="spectr_aggr"/>
        String e_name = desc.getAttributeValue("name"),
        e_id = desc.getAttributeValue("id"),
        e_template_name = desc.getAttributeValue("template"),
        e_entity_type = desc.getAttributeValue("entityType");

        //getData Template
        Template template = Controller.getInstance().
                    getProject().getTemplateByName(e_template_name);

        //getData entity description from tempalte
        Element entityDesc = template.getEntityParseParams(e_entity_type);
        //getData type of entity which will be searched
        String ch_templ = entityDesc.getChild("entity").getAttributeValue("template");
        String ch_type = entityDesc.getChild("entity").getAttributeValue("type");
        //create entity
        StatisticAggregatorEntity entity =
                new StatisticAggregatorEntity(template,e_entity_type,ch_templ,ch_type);


        if (e_id!=null) entity.setID(tools.parseInteger(e_id));
        entity.setName(e_name);
        try{
            //entity.onLoad();
        }catch(Exception e){

        }
        Controller.getInstance().addUpdateListener(entity);
        return entity;
    }

    public void update(Entity entity) throws UnsupportedEntityTypeException {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public Element save(Entity current) throws UnsupportedEntityTypeException {
        if (current instanceof StatisticAggregatorEntity){
            Element entity = new Element(ProjectFileHandler.ENTITY_TAG);
            entity.setAttribute(ProjectFileHandler.ENTITY_ATTR_NAME,current.getName());
            entity.setAttribute(ProjectFileHandler.ENTITY_ATTR_ID, current.getID().toString());
            entity.setAttribute(ProjectFileHandler.ENTITY_ATTR_TEMPLATE_NAME, current.getTemplate().getName());
            entity.setAttribute(ProjectFileHandler.ENTITY_ATTR_ENTITY_TYPE, current.getType());
            return entity;
        } else {
            throw new UnsupportedEntityTypeException();
        }
    }
}
